<?php

namespace Six;

use Swoole\Http\Server;

class App
{
    protected $server;

    //被监听的目录
    public static $rootPath;
    public static $framePath;
    public static $applicationPath;

    protected static $watch_path = [];
    //已加载的文件的散列值
    protected $md5FileStr;

    protected $timer_id;

    public function run()
    {
        self::$rootPath = dirname(dirname(__DIR__));
        self::$framePath = self::$rootPath . '/framework';
        self::$applicationPath = self::$rootPath . '/application';
        self::$watch_path = [self::$framePath, self::$applicationPath];

        $this->md5FileStr = $this->getMd5(); //初始化赋值
        //1.成够启动http服务
        $this->server = new Server('0.0.0.0', 9503);
        $this->server->set([
            'pack_max_length' => 1024 * 1024 * 2,
            'worker_num' => 2,
            // 'upload_tmp_dir'=>__DIR__."/upload",
            // 'document_root' =>__DIR__,
            // 'enable_static_handler' => true
        ]);
        $this->server->on('request', [$this, 'request']);
        $this->server->on('start', [$this, 'start']);
        $this->server->on('workerStart', [$this, 'workerStart']);
        $this->server->on('workerStop', [$this, 'workerStop']);
        $this->server->on('shutdown', [$this, 'shutdown']);
        $this->server->start();
    }

    public function request($request, $response)
    {
        $uri = $request->server['request_uri'];

        if ($uri == '/favicon.ico') {
            $response->status(404);
            $response->end();
        } else {

            $this->reload();
            $response->end('study swoole');
        }
    }

    public function start($server)
    {

        $this->reload();

    }

    public function workerStart($server, $worker_id)
    {


    }

    public function workerStop($server, $worker_id)
    {

//        swoole_timer_clear($this->timer_id);
//        echo '删除定时器';
    }

    public function shutdown(Server $server)
    {

    }

    protected function reload()
    {
        $id = swoole_timer_tick(3000, function () {
            $md5 = $this->getMd5();
            echo '新的'.$md5 . PHP_EOL;
            echo '旧的'.$this->md5FileStr . PHP_EOL;
            if ($this->md5FileStr != $md5) {
                echo '修改了'. PHP_EOL;
                $this->server->reload();
                $this->md5FileStr = $md5;
            }
        });
//        $this->timer_id = $id;
    }

    protected function getMd5()
    {
        $md5Str = '';
        foreach (self::$watch_path as $dir) {
            $md5Str .= self::md5File($dir);
        }
        return $md5Str;
    }

    protected static function md5File($dir)
    {
        if (!is_dir($dir)) {
            return '';
        }
        $md5File = array();
        $d = dir($dir);
        while (false !== ($entry = $d->read())) {
            if ($entry !== '.' && $entry !== '..') {
                if (is_dir($dir . '/' . $entry)) {
                    $md5File[] = self::md5File($dir . '/' . $entry);
                } elseif (substr($entry, -4) === '.php') {
                    $md5File[] = md5_file($dir . '/' . $entry);
                }
                $md5File[] = $entry;
            }
        }
        $d->close();
        return md5(implode('', $md5File));
    }
}

